package com.ywl.study.transaction.transaction;

import com.alibaba.fastjson.JSONObject;
import com.ywl.study.transaction.netty.NettyClient;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Slf4j
@Data
@Component
public class DistirctTransactionManager {
    private static NettyClient nettyClient;

    private static ThreadLocal<DistrictTransaction> current=new ThreadLocal<>();
    private static ThreadLocal<String> currentGroupId=new ThreadLocal<>();

    public static Map<String,DistrictTransaction> DISTRICT_TRANSACTION_MAP=new HashMap<>();

    @Autowired
    public void setNettyClient(NettyClient nettyClient){
        DistirctTransactionManager.nettyClient=nettyClient;
    }

    /*创建事务组，并返回groupid*/
    public static String getOrCreateGroup(){
        if(currentGroupId.get()!=null){
            return currentGroupId.get();
        }
        String groupId= UUID.randomUUID().toString();
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("groupId",groupId);
        jsonObject.put("command","create");
        currentGroupId.set(groupId);
        nettyClient.send(jsonObject);
        log.info("创建事务组");
        return groupId;
    }

    /**
     * 添加事务
     */
    public static void addDistrictTransaction(String groupId,TransactionType transactionType){
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("groupId",groupId);
        jsonObject.put("transactionType",transactionType);
        jsonObject.put("command","add");
        String transactionId=UUID.randomUUID().toString();
        jsonObject.put("transactionId",transactionId);
        DistrictTransaction transaction=new DistrictTransaction(groupId,transactionId,transactionType);
        DISTRICT_TRANSACTION_MAP.put(groupId,transaction);
        current.set(transaction);
        nettyClient.send(jsonObject);
        log.info("添加事务");
    }


    public static void sendTxOver() throws Exception {
        if(currentGroupId.get()==null){
            throw new Exception("事务没有创建好");
        }

        DistrictTransaction currentTrasaction=current.get();
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("groupId",currentTrasaction.getGroupId());
        jsonObject.put("command","over");
        nettyClient.send(jsonObject);
        log.info("通知事务注册已经结束");
    }

    public static DistrictTransaction getTransaction(String groupId){
        return DISTRICT_TRANSACTION_MAP.get(groupId);
    }


    public static DistrictTransaction getCurrentTransaction(){
        return current.get();
    }

    public static void setCurrentTrasaction(DistrictTransaction trasaction){
         current.set(trasaction);
    }

    public static String getCurrentGroupId(){
        return currentGroupId.get();
    }
    public static void setCurrentGroupId(String groupId){
        currentGroupId.set(groupId);
    }



























}
